#!/bin/bash
# 设置redis软件目录
redis_home="/usr/local/redis"
soft_dir="/opt/redis"
echo "创建软件包目录"$soft_dir $redis_home
if [ ! -d "$soft_dir" ]; then
  mkdir $soft_dir
fi
if [ ! -d "$redis_home" ]; then
  mkdir $redis_home
fi
########################################################
#############       安装 redis-5.0.0     ################
########################################################
cd $soft_dir || exit
if [ -e "redis-5.0.0.tar.gz" ]; then
  echo "文件已存在，无需重复下载！"
else
  echo "执行下载解压redis！"
  wget https://repo.huaweicloud.com/redis/redis-5.0.0.tar.gz
fi
tar -zxvf redis-5.0.0.tar.gz
cd redis-5.0.0/deps || exit
make hiredis lua jemalloc linenoise
cd ..
make
make install PREFIX=/usr/local/redis
echo "复制配置文件到安装目录！"
cp redis.conf /usr/local/redis/bin
cd /usr/local/redis/bin || exit
echo "修替换默认配置文件信息！"
sed -i "s/daemonize no/daemonize yes/g" redis.conf
sed -i "s/bind 127.0.0.1/bind 0.0.0.0 /g" redis.conf
sed -i "s/# requirepass foobared/requirepass 4redis@Invariable/g" redis.conf
echo "启动redis server！"
./redis-server /usr/local/redis/bin/redis.conf
sleep 1s
echo "配置redis server开机启动！"
echo "/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf" >>/etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
echo "默认redis连接密码：4redis@Invariable"
echo "关闭redis服务：pkill redis-server"
echo "启动redis服务：./redis-server /usr/local/redis/bin/redis.conf"
